﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Baosight.ZNH.ZMCommon.Validators.Filters
{
    public partial class TipWindow : UserControl
    {
        private FrameworkElement rootVisual;
        private Control placementTarget;
        private bool isShow;

        public bool IsShow
        {
            get { return isShow; }
            set { isShow = value; }
        }

        public Control PlacementTarget
        {
            get { return placementTarget; }
            set { placementTarget = value; }
        }

        public TipWindow()
        {
            InitializeComponent();
            IsShow = true;
        }

        public void SetDisplayValue(string meg)
        {
            this.mainContent.Text = meg;
        }

        internal FrameworkElement RootVisual
        {
            get
            {
                SetRootVisual();
                return rootVisual;
            }
        }

        private void SetRootVisual()
        {
            if ((rootVisual != null) || (Application.Current == null))
            {
                return;
            }

            rootVisual = Application.Current.RootVisual as FrameworkElement;
            if (rootVisual == null)
            {
                return;
            }

            rootVisual.SizeChanged += OnRootVisualSizeChanged;
        }

        private void OnRootVisualSizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.tip.IsOpen = false;
        }

        private void PerformPlacement(double horizontalOffset, double verticalOffset, double width)
        {
            //calculate position based on browser zoom factor
            var browserZoomFactor = Application.Current.Host.Content.ZoomFactor;

            var offsetX = (horizontalOffset + width + 5) * browserZoomFactor;
            var offsetY = verticalOffset * browserZoomFactor;

            offsetX = Math.Max(2.0, offsetX);
            offsetY = Math.Max(2.0, offsetY);

            //get actual and previous dimensions
            var actualHeight = RootVisual.ActualHeight * browserZoomFactor;
            var actualWidth = RootVisual.ActualWidth * browserZoomFactor;
            var lastHeight = 23;
            var lastWidth = this.mainContent.ActualWidth + 4;

            var lastRectangle = new Rect(offsetX, offsetY, lastWidth, lastHeight);
            var actualRectangle = new Rect(0.0, 0.0, actualWidth, actualHeight);
            actualRectangle.Intersect(lastRectangle);

            if ((Math.Abs(actualRectangle.Width - lastRectangle.Width) > 2.0))
            {
                this.tip.HorizontalOffset = (horizontalOffset - lastWidth - 5) * browserZoomFactor;
                this.tip.VerticalOffset = offsetY;
            }
            else
            {
                this.tip.HorizontalOffset = offsetX;
                this.tip.VerticalOffset = offsetY;
            }
        }

        public void BindControl(Control uc)
        {
            PlacementTarget = uc;
            PlacementTarget.GotFocus += new RoutedEventHandler(uc_GotFocus);
            PlacementTarget.LostFocus += new RoutedEventHandler(uc_LostFocus);

            var horizontal = PlacementTarget.TransformToVisual(null).Transform(new Point(0, 0)).X;
            var vertical = PlacementTarget.TransformToVisual(null).Transform(new Point(0, 0)).Y;
            var width = PlacementTarget.ActualWidth;

            PerformPlacement(horizontal, vertical, width);
        }

        public void CancelBindControl()
        {
            PlacementTarget.GotFocus -= new RoutedEventHandler(uc_GotFocus);
            PlacementTarget.LostFocus -= new RoutedEventHandler(uc_LostFocus);
        }

        /// <summary>
        /// 获得焦点时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uc_GotFocus(object sender, RoutedEventArgs e)
        {
            if (IsShow == true)
            {
                var horizontal = PlacementTarget.TransformToVisual(null).Transform(new Point(0, 0)).X;
                var vertical = PlacementTarget.TransformToVisual(null).Transform(new Point(0, 0)).Y;
                var width = PlacementTarget.ActualWidth;

                PerformPlacement(horizontal, vertical, width);
                this.tip.IsOpen = true;
            }
        }


        private void uc_LostFocus(object sender, RoutedEventArgs e)
        {
            this.tip.IsOpen = false;
        }
    }
}
